layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    var laydate = layui.laydate;

    table.init('zmn-table-plat', {
        toolbar: "#zmn-table-plat-tool",
        page: false,
        height: 'full-140',
        defaultToolbar: [],
        done: function (res, curr, count) {
            //添加按钮
            toolbarRightAddButton([{
                title: '帮助',
                layEvent: 'tableHelp',
                icon: 'layui-icon-help'
            }]);
        }
    });

    table.init('zmn-table-company', {
        toolbar: "#zmn-table-company-tool",
        page: false,
        height: 'full-140',
        defaultToolbar: [],
        done: function (res, curr, count) {
            //添加按钮
            toolbarRightAddButton([{
                title: '帮助',
                layEvent: 'tableHelp',
                icon: 'layui-icon-help'
            }]);
        }
    });

    function toolbarRightAddButton(layout) {
        let iconElem = [];
        layout.filter((i, v) => i.display || i.display === undefined).forEach((item, index, array) => {
            iconElem.push('<div class="layui-inline" title="' + item.title + '" lay-event="' + item.layEvent + '">'
                + '<i class="layui-icon ' + item.icon + '"></i>'
                + '</div>');
        });
        //  替换容器内容
        document.querySelector('.layui-table-tool-self').innerHTML = iconElem.join('');
    }

    // 表格操作按钮
    table.on("toolbar(zmn-table-plat)", function (obj) {
        var ids = zmnPage.layTabldIds(obj, "distributePlatId");
        if (obj.event == "add") {
            // 目前只有平台派单有新增 这里先写死 distributeWay=1
            zmnLayUtil.openIframeCustom(layer, '/orderconfig/distribute/edit?distributeWay=10&bizType=' + json.bizType ,
                {
                    title: '修改配置',
                    shade: 0.4,
                    area: ['80%', '80%']
                }
            );
        }
        if (obj.event == "enable") {
            if (ids.length === 0) {
                return layer.alert("请选择至少一个要操作的配置！");
            }
            updateStatus(ids.join(","), 2);
        }
        if (obj.event == "disable") {
            if (ids.length === 0) {
                return layer.alert("请选择至少一个要操作的配置！");
            }
            updateStatus(ids.join(","), 1);
        }
        if (obj.event !== "tableHelp") {
            return;
        }

        if (obj.event == 'tableHelp') {
            tips(help_content.innerHTML, this);
        }
    });

    function tips(html, ele) {
        layui.layer.open({
            type: 4
            , closeBtn: false
            , shade: 0.0001
            , tips: 1
            , skin: 'zmn-tips'
            , shadeClose: true
            , content: [html, ele]
        });
    }

    form.on('switch(changeStatus)', function(data){
        var value = this.checked ? 2 : 1;
        var ids = new Array();
        ids[0] = data.elem.id;
        updateStatus(ids.join(","), value);
    });

    function updateStatus(idList, status) {

        layer.alert('你确定要修改状态吗', {
            skin: 'layui-layer-molv'
            , closeBtn: 1
            , anim: 1
            , btn: ['确定', '取消']
            , icon: 6
            , yes: function () {
                zmnPage.ajaxSimple("/orderconfig/distribute/ajax/updateStatus", {idList: idList, status: status}, 1000);
            }
            , btn2: function () {
                layer.msg('您取消了状态修改');
            }
        });

    }

    // 表格内操作按钮
    table.on('tool(zmn-table-plat)', function (obj) {toolOperation(obj)});

    table.on('tool(zmn-table-company)', function (obj) {toolOperation(obj)});

    function toolOperation (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        var distributeWay = data.way;
        if (layEvent === 'platConfig') {
            zmnLayUtil.openIframeCustom(layer, '/orderconfig/distribute/edit?distributePlatId=' + data.distributePlatId + '&distributeWay=' + distributeWay + '&bizType=' + json.bizType,
                {
                    title: '修改配置',
                    shade: 0.4,
                    area: ['80%', '80%']
                }
            );
        } else if (layEvent === 'companyConfig') {
            zmnLayUtil.openTabsPageCustom(parent, '/orderconfig/distribute/company/list.action?plat='+data.plat+'&pageSize=100&bizType=' + json.bizType, '服务商派单参数配置');
        } else if (layEvent === 'openCity') {
            layer.open({
                type: 2
                , title: "开通城市"
                , area: ['80%', '80%']
                , btn: []
                , content: '/orderconfig/distribute/cityConfig.action?distributePlatId=' + data.distributePlatId
                , success: function (layero, index) {
                }
                , end: function () {
                    zmnPage.reload("", 1);
                }
            });
        }
    }

    //对外暴露的接口
    exports('orderconfig_distribute_f_list', {});
});